<?php
class article extends Admin {

    private $sort = null;
    private $limit = 10;
    private $name = "文章";
    private $tab = array( 'article' => '文章', 'page' => '页面', 'link' => '链接' );
    private $top = array( 1 => '取消置顶', 0 => '置顶' );
    private $viewDate = array(
        '3600' => '1小时',
        '21600' => '12小时',
        '86400' => '1天',
        '259200' => '3天',
        '604800' => '1周',
        '2592000' => '1月',
        '7948800' => '3月',
    );
    private $viewOrder = array(
        '最多浏览' => 'views desc',
        '最少浏览' => 'views asc',
        'divider1' => '',
        '最多评论' => 'comments desc',
        '最少评论' => 'comments asc',
        'divider2' => '',
        '时间升序' => 'dateline desc',
        '时间降序' => 'dateline asc',
    );

    private $viewAttr = array( 'isTop' => '置顶', 'isHide' => '隐藏', 'isLock' => '锁定', );

    public function __construct() {
        parent::__construct();
        $this->sort = $this->sort();
        $this->type = R( 'type', 'article' );
        if ( $this->type == 'page' ) {
            $this->sort = $this->Node()->dataTreeArray( $this->get_page() );
        }
        $this->name = $this->tab[ $this->type ];
    }

    private function get_title( $title ) {
        if ( $key = R( 'key' ) ) {
            return str_replace( $key, "<b style=\"color: blue;\">$key</b>", $title );
        }
        return $title;
    }

    private function getand( $a ) {
        return empty( $a ) ? '' : ' and ';
    }

    public function index( $c, $a ) {
        if ( $this->type != 'page' ) {

            $this->model->table = $c; // 数据库表名称

            $where = "model = '{$this->type}'";
            $sort = 'isTop desc, dateline desc';

            // 拼接查询条件
            if ( $dateline = R( 'dateline' ) ) {
                $dateline = time() - $dateline;
                $where .= "{$this->getand( $where)}dateline > {$dateline}";
            }
            if ( $sid = intval( R( 'sid' ) ) ) {
                $where .= "{$this->getand( $where)}sid = {$sid}";
            }
            if ( $k = R( 'k' ) ) {
                $where .= "{$this->getand( $where)}{$k} = 1";
            }
            if ( $order = R( 'order' ) ) {
                $sort = "{$order}";
            }
            if ( $key = R( 'key' ) ) {
                $where .= "{$this->getand( $where)}title like '%{$key}%'";
            }

            $result = $this->Pager( $this->limit )->findAll( $where, $sort );
            //echo $model->dumpSql();
        }

        $tpl = $this->type == $c ? $c : "{$c}_{$this->type}";
        include $this->display( $tpl );
    }

    // 添加文章
    public function add( $c, $a ) {
        $tpl = $this->type == $c ? $c : "{$c}_{$this->type}";
        include $this->display( $tpl );
    }

    // 编辑文章
    public function edit( $c, $a ) {
        if ( !$id = intval( R( "id" ) ) ) {
            $this->error();
        }

        // 读取文章
        $this->model->table = $c;
        $result = $this->model->find( array( 'id' => $id ) );

        // 读取文章标签
        $result[ 'tag' ] = $this->model->tag_id( $id );

        $tpl = $this->type == $c ? $c : "{$c}_{$this->type}";
        include $this->display( $tpl );
    }

    // 保存文章
    public function save( $c, $a ) {
        // 获取表单数据
        foreach ( $_POST[ $c ] as $key => $val ) {
            if ( $key == 'alias' ) $val = str_replace( " ", "-", strtolower( $val ) );
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        $rows[ 'dateline' ] = strtotime( $rows[ 'dateline' ] );
        $rows[ 'isTop' ] = isset( $rows[ 'isTop' ] ) ? $rows[ 'isTop' ] : 0;
        $rows[ 'isHide' ] = isset( $rows[ 'Hide' ] ) ? $rows[ 'Hide' ] : 0;
        $rows[ 'isLock' ] = isset( $rows[ 'isLock' ] ) ? $rows[ 'isLock' ] : 0;

        // 获取上传附件总数
        $attach = explode( ",", $_POST[ 'attach' ][ 'id' ] );
        $attach_total = $attach ? ( count( $attach ) - 1 ) : 0;

        $this->model->table = $c;

        if ( $id = intval( R( "id" ) ) ) {
            // 修改文章
            $this->model->update( array( 'id' => $id ), $rows );

            // 修改文章附件计数
            $this->model->incrField( array( 'id' => $id ), 'attachments', $attach_total );

            // 更新文章标签
            $this->model->editTag( $id );
        } else {

            $rows[ 'attachments' ] = $attach_total;

            // 添加文章
            $id = $this->model->create( $rows );

            // 对文章分类计数 +1
            $this->model->table = "sort";
            $this->model->incrField( array( 'id' => $rows['sid'] ), 'total' );

            // 添加文章标签
            $this->model->addTag( $id );
        }


        // 修改附件文章ID
        $attach = $_POST[ 'attach' ][ 'id' ];
        if ( $attach ) {
            $attach = rtrim( $attach, "," );
            $this->model->table = "attachment";
            $this->model->update( "id in ({$attach})", array( 'aid' => $id ) );
        }

        // 更新页面数据
        if ( $rows[ 'model' ] == 'page' ) {
            $this->model->Cache()->update( 'page' );
        }

        // 执行扩展
        $rows[ 'id' ] = $id;
        $this->doAction( 'add_post', $rows );

        $this->success( "保存文章成功!" );
    }

    // 删除文章
    public function del( $c ) {
        if ( !$id = intval( R( "id" ) ) ) {
            $this->error();
        }

        // 删除文章
        $this->model->table = $c;
        $this->model->delete( array( "id" => $id ) );

        // 删除标签
        $this->model->table = 'tag';
        $this->model->delete( array( "aid" => $id ) );

        // 删除附件
        $this->model->table = 'attachment';
        $this->model->delete( array( "aid" => $id ) );

        $this->success( '删除文章成功!' );
    }

    // 批量删除文章
    public function delAll() {
        if ( !$in_id = R( 'in_id' ) ) {
            $this->error();
        }

        // 删除文章
        $this->model->table = 'article';
        $this->model->delete( "id in ({$in_id}) " );

        // 删除标签
        $this->model->table = 'tag';
        $this->model->delete( "aid in ({$in_id})" );

        // 删除附件
        $this->model->table = 'attachment';
        $this->model->delete( "aid in ({$in_id})" );

        $this->success( '删除文章ID {$in_id}成功!' );
    }

    // 置顶文章
    public function top( $c ) {
        if ( !$id = intval( R( "id" ) ) ) {
            $this->error();
        }

        $v = R( 'v' ) ? 0 : 1;
        $this->model->table = $c;
        $this->model->update( array( 'id' => $id ), array( 'isTop' => $v ) );
        header( "location:" . $this->url( $c, null, array( 'type' => $this->type ) ) );
    }

    // 批量显示隐藏文章
    public function hide() {
        $this->operates( 'isHide', '批量显示隐藏文章' );
    }

    // 批量移动文章
    public function mv() {
        $this->operates( 'sid', '批量移动文章' );
    }

    // 批量置顶文章
    public function topAll() {
        $this->operates( 'isTop', '批量置顶文章' );
    }

    // 批量操作
    private function operates( $key, $echo ) {
        if ( !$in_id = R( 'in_id' ) ) {
            $this->error();
        }
        $this->model->table = 'article';
        $this->model->update( "id in ({$in_id}) ", array( $key => R( 'v' ) ) );

        $this->success( "{$echo}成功" );
    }
}